package mbms.mobilebudget.bankaccount;

import java.util.List;

import mbms.mobilebudget.R;
import mbms.mobilebudget.UserManager;
import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.BankAccount;
import mbms.mobilebudget.entity.User;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.j256.ormlite.android.apptools.OrmLiteBaseListActivity;


public class BankAccountsActivity  extends OrmLiteBaseListActivity<DatabaseHandler> {

	private User user;
    private BankAccountAdapter bankAccountArrayAdapter;
	private UserManager userManager;
	private BankAccountManager bankAccountManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_bank_accounts);
		
		Intent intent = getIntent();
		user = (User) intent.getSerializableExtra("loggedInUser");
		if(user==null){
			Toast.makeText(getApplicationContext(), 
					"User not acquired", Toast.LENGTH_LONG).show();
			return ;
		}
		
		userManager = new UserManager(getHelper(), getApplicationContext(), user);
		bankAccountManager = new BankAccountManager(getHelper(), getApplicationContext());
		List<BankAccount> userBankAccounts = userManager.getBankAccountList();
		
		bankAccountArrayAdapter = new BankAccountAdapter(this, R.layout.bank_account_row, userBankAccounts);
		setListAdapter(bankAccountArrayAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				BankAccount bankAccount = bankAccountArrayAdapter.getItem(position);
				
				startBankAccountsAddUpdateActivity(bankAccount);
			}
		});
		
		registerForContextMenu(getListView());
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.bank_accounts, menu);
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      BankAccount bankAccount = bankAccountArrayAdapter.getItem(info.position);

      switch(item.getItemId()) {
      case R.id.edit:
    	  	startBankAccountsAddUpdateActivity(bankAccount);
            return true;
      case R.id.delete:
    	    bankAccountManager.deleteBankAccount(bankAccount);
    	    bankAccountArrayAdapter.remove(bankAccount);
    	    setListAdapter(bankAccountArrayAdapter);
    	    Toast.makeText(getApplicationContext(), "Delete successful", Toast.LENGTH_SHORT).show();
    	    return true;
      default:
            return super.onContextItemSelected(item);
      }
    }
	
	public void showInsertBankAccountActivity(View view) {
		BankAccount bankAccount = new BankAccount();
		bankAccount.setUser(user);
		
		startBankAccountsAddUpdateActivity(bankAccount);
	}
	
	public void backButtonClicked(View view){
		super.onBackPressed();
	} 
	
	private void startBankAccountsAddUpdateActivity(BankAccount bankAccount) {
		Intent intent = new Intent(this, BankAccountsAddUpdateActivity.class);
		intent.putExtra("bankAccount", bankAccount);
		startActivity(intent);
	}
   @Override
    public void onResume()
    {
        super.onResume();
 		List<BankAccount> userBankAccounts = userManager.getBankAccountList();
		
 		bankAccountArrayAdapter = new BankAccountAdapter(this, R.layout.bank_account_row, userBankAccounts);
 		setListAdapter(bankAccountArrayAdapter);
 		
 		getListView().setOnItemClickListener(new OnItemClickListener() {
 			@Override
 			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 				BankAccount bankAccount = bankAccountArrayAdapter.getItem(position);
 				
 				startBankAccountsAddUpdateActivity(bankAccount);
 			}
 		});
 		
 		registerForContextMenu(getListView());
    }
}


